package com.donfu.web.controller.house;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseExclusiveservice;
import com.donfu.system.service.house.IHouseExclusiveserviceService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

import javax.xml.bind.util.JAXBResult;

/**
 * 我的导购Controller
 * 
 * @author donfu
 * @date 2021-12-01
 */
@Api(tags = "后台我的导购")
@RestController
@RequestMapping("/house/exclusiveservice")
public class HouseExclusiveserviceController extends BaseController
{
    @Autowired
    private IHouseExclusiveserviceService houseExclusiveserviceService;

    /**
     * 查询我的导购列表
     */
    @ApiOperation("查询我的导购列表")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseExclusiveservice houseExclusiveservice)
    {
        startPage();
        List<HouseExclusiveservice> list = houseExclusiveserviceService.selectHouseExclusiveserviceList(houseExclusiveservice);
        return getDataTable(list);
    }

    /**
     * 导出我的导购列表
     */
    @ApiOperation("导出我的导购")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:export')")
    @Log(title = "我的导购", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseExclusiveservice houseExclusiveservice)
    {
        List<HouseExclusiveservice> list = houseExclusiveserviceService.selectHouseExclusiveserviceList(houseExclusiveservice);
        ExcelUtil<HouseExclusiveservice> util = new ExcelUtil<HouseExclusiveservice>(HouseExclusiveservice.class);
        return util.exportExcel(list, "我的导购数据");
    }

    /**
     * 获取我的导购详细信息
     */
    @ApiOperation("获取详情")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseExclusiveserviceService.selectHouseExclusiveserviceById(id));
    }

    /**
     * 新增我的导购
     */
    @ApiOperation("新增我的导购")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:add')")
    @Log(title = "我的导购", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseExclusiveservice houseExclusiveservice)
    {
        AjaxResult ajaxResult = checkHouseExclusive(houseExclusiveservice);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseExclusiveserviceService.insertHouseExclusiveservice(houseExclusiveservice));
    }

    /**
     * 修改我的导购
     */
    @ApiOperation("修改我的导购")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:edit')")
    @Log(title = "我的导购", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseExclusiveservice houseExclusiveservice)
    {
        Long id = houseExclusiveservice.getId();
        if(id==null){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult ajaxResult = checkHouseExclusive(houseExclusiveservice);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseExclusiveserviceService.updateHouseExclusiveservice(houseExclusiveservice));
    }

    /**
     * 删除我的导购
     */
    @ApiOperation("删除我的导购")
    @PreAuthorize("@ss.hasPermi('house:exclusiveservice:remove')")
    @Log(title = "我的导购", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseExclusiveserviceService.deleteHouseExclusiveserviceByIds(ids));
    }

    /**
     * 检测参数是否合格
     * @param houseExclusiveservice
     * @return
     */
    public AjaxResult checkHouseExclusive(HouseExclusiveservice houseExclusiveservice){
        Long shopperId = houseExclusiveservice.getShopperId();
        if(shopperId==null || shopperId.equals("")){
            return AjaxResult.error("导购不能为空");
        }
        Long storeId = houseExclusiveservice.getStoreId();
        if(storeId==null || storeId.equals("")){
            return AjaxResult.error("店铺id不能为空");
        }
        Long userId = houseExclusiveservice.getUserId();
        if(userId==null || userId.equals("")){
            return AjaxResult.error("用户Id不能为空");
        }
        return AjaxResult.success();
    }
}
